home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / SORTING.SWG / 0013_OOP-SORT.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  1KB  |  48 lines

  1. {
  2. WL> Say, would anyone know how-to sort a Record With 5 thing
  3.  WL> in it one of which is "NAME"...I want to sort each Record
  4.  WL> in the Array by name and can't figure it out....my Array
  5.  WL> name is LabelS and my Record name is SofT....so any help
  6.  WL> would greatly be appreciated...thanks
  7.  
  8. The easiest way is to make it an Object, and put it in a TSortedCollection.
  9. For example:
  10. }
  11.  
  12.   Type
  13.     PMyrec = ^TMyrec;
  14.     TMyrec = Object(tObject)
  15.       name : String;
  16.       other : Integer;
  17.     end;
  18.  
  19.     TSortedRecs = Object(TSortedCollection)
  20.       Function Compare(Key1,key2:Pointer):Integer; Virtual;
  21.     end;
  22.  
  23.   Function TSortedRecs.Compare;
  24.   Var
  25.     p1 : PMyrec Absolute Key1;
  26.     p2 : PMyrec Absolute Key2;
  27.   begin
  28.     if p1^.name < p2^.name then
  29.       Compare := -1
  30.     else if p1^.name = p2^.name then
  31.       Compare := 0
  32.     else
  33.       Compare := 1;
  34.   end;
  35.  
  36. Var
  37.   rec : PMyrec;
  38.   coll: TSortedRecs; begin
  39.   coll.init(100,10);   { Init to 100 Records, grow by 10s }
  40.  
  41.   While More_Records do
  42.   begin
  43.     new(rec,init);
  44.     rec^.name := Get_Name;
  45.     rec^.other:= Get_Other;
  46.     coll.insert(rec);
  47.   end;
  48.